package com.closer.server.api.admin;

import com.closer.server.biz.images.dto.ImageVO;
import com.closer.server.biz.images.dto.SearchRequest;
import com.closer.server.biz.user.dto.vo.UserVO;
import com.closer.server.support.common.BaseResponse;
import com.closer.server.support.common.ResultUtils;
import com.mybatisflex.core.paginate.Page;
import org.springframework.web.bind.annotation.*;
import com.closer.server.biz.images.service.ImagesService;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;

/**
 * 图片资源 控制层。
 *
 * @author closer
 */
@RestController
@RequestMapping("/admin/images")
public class AdminImagesController {

    private final ImagesService imageService;

    public AdminImagesController(ImagesService imageService){
        this.imageService = imageService;
    }

    @PostMapping("/upload")
    public BaseResponse<Long> uploadImage(String type, MultipartFile uploadFile) throws IOException {
        InputStream file = null;
        file = uploadFile.getInputStream();
        var imageId = imageService.upload(type, file);
        return ResultUtils.success(imageId);
    }

    @PostMapping("/enable")
    public BaseResponse<Boolean> enable(@RequestParam Long id) {
        imageService.enable(id);
        return ResultUtils.success(true);
    }

    @PostMapping("/disable")
    public BaseResponse<Boolean> disable(@RequestParam Long id) {
        imageService.disable(id);
        return ResultUtils.success(true);
    }

    @PostMapping("/delete")
    public BaseResponse<Boolean> delete(@RequestParam Long id) {
        imageService.delete(id);
        return ResultUtils.success(true);
    }

    @PostMapping("/search")
    public BaseResponse<Page<ImageVO>> search(@RequestBody SearchRequest params){
        var list = imageService.search(params);
        return ResultUtils.success(list);
    }

}
